home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: lut.ac.uk!usenet
- From: COSJM1 <S.J.Morgan-92@student.lut.ac.uk>
- Subject: Problem with Templates
- Sender: usenet@lut.ac.uk (Usenet-News)
- Message-ID: <DMy82D.Irt@lut.ac.uk>
- Date: Sun, 18 Feb 1996 01:56:37 GMT
- X-Nntp-Posting-Host: co-pclab39.lut.ac.uk
- Content-Transfer-Encoding: 7bit
- Content-Type: text/plain
- Mime-Version: 1.0
- X-Mailer: Mozilla 1.2N (Windows; I; 16bit)
- Organization: Loughborough University
-
- Hi,
- I'm currently writing my final year project in C++. It's spread
- over 17 modules and when using templates for a linked list class, it
- compiles alright but won't link correctly. The code has to be portable
- so I've tryed CFront 3.0, Borland 3.1,4.x and GCC 2.7.0 on a Sun Sparc10:
- they all give the same sort of link error.
-
- Below is a small test program which demonstrates the priniples:
-
- IN FILE "TEMPLATE.H":
-
- template<class T> class A
- {
- public:
- A() {}
- private:
-
- };
-
- template<class T> class B
- {
- public:
- B() {}
- void test(void);
-
- private:
- A<T> *include_one;
- };
-
- IN FILE "TEMPLATE2.CPP":
-
- #include <iostream.h>
- #include <template.h>
-
- template<class T> void B<T>::test(
- void )
- {
- cout << "hello";
- }
-
- IN FILE "TEMPLATE.CPP":
-
- #include <iostream.h>
-
- #include "template.h"
-
- main()
- {
- B<int*>
- test;
-
- test.test();
- }
-
- I get the following linking error:
-
- (Error) Undefined symbol(s)
- B<int*>::test(void), referred to from template.o
-
- I think it's to do with the scope when it gets instantiated because when I
- include all the above in one file, it compiles and links correctly. Obviously
- with 17 modules I don't want to put over 5K lines of source into one file!
-
- I would appreciate any advice on how best to proceed.
-
- Thank you in advance for any help given.
-
- Steve.
-
-